package ua.com.globallogic.javatraining.iryna_chorna.univercity.structure;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;


/**
 * class Auditorium models an <code>University</code>
 * class room. Auditorium has a unique number. It can hold a specific 
 * number of students. Auditorium can be booked or freed
 */
public class Auditorium {
	public static final int MAX_LESSON_NUMBER_PER_DAY = 10;
	
	private static int nextNumber = 1;
	private int number;
	private int capacity;
	//TODO add javadoc for bookingShedule? 
	private Map<GregorianCalendar, int[]> bookingSchedule = new HashMap<GregorianCalendar, int[]>();
	
	/**
	 * Constructs an auditorium with unique number and specific capacity
	 * @param capacity amount of students the auditorium can contain
	 */
	public Auditorium(int capacity) {
		this.number = nextNumber;
		nextNumber++;
		this.capacity = capacity;
	}
	
	/**
	 * Books an auditorium for a lesson number on a concrete date
	 * @param date a day to book auditorium on
	 * @param lessonNumber sequence number of the lesson during which auditorium will be booked
	 * @return true if auditorium was successfully booked 
	 */
	public boolean book(GregorianCalendar date, int lessonNumber) {
		if (!bookingSchedule.containsKey(date)) {
			bookingSchedule.put(date, new int[MAX_LESSON_NUMBER_PER_DAY]);
		}
		int[] bookedLesson = bookingSchedule.get(date);
		if ((lessonNumber <= MAX_LESSON_NUMBER_PER_DAY) && !isBooked(date, lessonNumber)) {
			bookedLesson[lessonNumber - 1] = lessonNumber;
			System.out.println("Auditorium " + number + " has been successfully booked for " + lessonNumber + " lesson");
			return true;
		} else {
			System.out.println("Booking denied: auditorium " + number + " is already booked for " + lessonNumber + " lesson");
			return false;
		}
	}
	
	
	/**
	 * Checks whether the auditorium is booked for a specific date and lesson
	 * @param date date to check the auditorium availability 
	 * @param lessonNumber sequence number of the lesson
	 * @return true if the auditorium is booked for the specified date
	 */
	public boolean isBooked(GregorianCalendar date, int lessonNumber) {
		if (bookingSchedule.containsKey(date)) {
			int[] bookedLessons = bookingSchedule.get(date);
			if (bookedLessons[lessonNumber - 1] == lessonNumber) {
				System.out.println("auditorium is booked");
				return true;
			}
		}
		System.out.println("auditorium is completely free on " + date.get(Calendar.YEAR) + "." + date.get(Calendar.MONTH)
				+ "." + date.get(Calendar.DATE));
		return false;
	}
	
	/**
	 * Frees auditorium for a time slot that corresponds to specified lesson number on specified date
	 * @param date during which auditorium should be free
	 * @param lessonNumber sequence number of the lesson during which auditorium should be free
	 */
	public void free(GregorianCalendar date, int lessonNumber) {
		if (isBooked(date, lessonNumber)) {
			int[] bookedLessons = bookingSchedule.get(date);
			bookedLessons[lessonNumber - 1] = 0; 
		}
	}

	/**
	 * Frees auditorium for a complete day
	 * @param date a date when auditorium should be free
	 */
	public void free (GregorianCalendar date) {
		if (bookingSchedule.containsKey(date)) {
			int[] bookedLessons = bookingSchedule.get(date);
			for (int i : bookedLessons) {
				bookedLessons[i] = 0;
			}
		}
	}
}
